﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace SanctusSoftware.Music.ChordSheetEditor.Word
{
    public static class DocumentSetupCopier
    {

        #region Styles

        public static void CopyStyles(Document sourceDocument, Document targetDocument)
        {
            Dictionary<string, Style> sourceStyles = new Dictionary<string, Style>();

            foreach (Style sourceStyle in sourceDocument.Styles)
            {
                if (sourceStyle.InUse)
                {
                    sourceStyles.Add(sourceStyle.NameLocal, sourceStyle);
                }
            }

            Dictionary<string, Style> targetStyles = new Dictionary<string, Style>();

            foreach (Style targetStyle in targetDocument.Styles)
            {
                if (targetStyle.InUse)
                {
                    targetStyles.Add(targetStyle.NameLocal, targetStyle);
                }
            }

            foreach (string key in targetStyles.Keys)
            {
                if (sourceStyles.ContainsKey(key))
                {
                    DocumentSetupCopier.CopyStyle(sourceStyles[key], targetStyles[key]);
                }
                else
                {
                    // TODO: Define it
                }
            }
        }

        public static void CopyStyle(Style sourceStyle, Style targetStyle)
        {
            try
            {
                CopyStyleFont(sourceStyle, targetStyle);

                CopyStyleParagraphFormat(sourceStyle, targetStyle);


            }
            catch (Exception ex)
            {

                throw;
            }


            //targetStyle.AutomaticallyUpdate = sourceStyle.AutomaticallyUpdate;
            //targetStyle.Borders 
            //targetStyle.Description = sourceStyle.Description;
            //targetStyle.Font = sourceStyle.Font;
            //targetStyle.Frame 
            //targetStyle.LanguageID = sourceStyle.LanguageID;
            //targetStyle.LanguageIDFarEast = sourceStyle.LanguageIDFarEast;
            //targetStyle.Linked = sourceStyle.Linked;
            //targetStyle.ListLevelNumber = sourceStyle.ListLevelNumber;
            //targetStyle.NoProofing = sourceStyle.NoProofing;
            //targetStyle.NoSpaceBetweenParagraphsOfSameStyle = sourceStyle.NoSpaceBetweenParagraphsOfSameStyle;
            //targetStyle.ParagraphFormat 
            //targetStyle.Priority = sourceStyle.Priority;
            //targetStyle.QuickStyle = sourceStyle.QuickStyle;
            //targetStyle.Shading = sourceStyle.Shading;
            //targetStyle.Table = sourceStyle.Table;
            //targetStyle.Type = sourceStyle.Type;
            //targetStyle.UnhideWhenUsed = sourceStyle.UnhideWhenUsed;
            //targetStyle.Visibility = sourceStyle.Visibility;
        }

        private static void CopyStyleParagraphFormat(Style sourceStyle, Style targetStyle)
        {
            if (sourceStyle.Type == WdStyleType.wdStyleTypeParagraph)
            {

                targetStyle.ParagraphFormat = sourceStyle.ParagraphFormat;
                return;
                
                /*
                targetStyle.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha = sourceStyle.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha;
                targetStyle.ParagraphFormat.AddSpaceBetweenFarEastAndDigit = sourceStyle.ParagraphFormat.AddSpaceBetweenFarEastAndDigit;
                targetStyle.ParagraphFormat.Alignment = sourceStyle.ParagraphFormat.Alignment;
                targetStyle.ParagraphFormat.AutoAdjustRightIndent = sourceStyle.ParagraphFormat.AutoAdjustRightIndent;
                targetStyle.ParagraphFormat.BaseLineAlignment = sourceStyle.ParagraphFormat.BaseLineAlignment;
                targetStyle.ParagraphFormat.CharacterUnitFirstLineIndent = sourceStyle.ParagraphFormat.CharacterUnitFirstLineIndent;
                targetStyle.ParagraphFormat.CharacterUnitLeftIndent = sourceStyle.ParagraphFormat.CharacterUnitLeftIndent;
                targetStyle.ParagraphFormat.CharacterUnitRightIndent = sourceStyle.ParagraphFormat.CharacterUnitRightIndent;
                targetStyle.ParagraphFormat.DisableLineHeightGrid = sourceStyle.ParagraphFormat.DisableLineHeightGrid;
                targetStyle.ParagraphFormat.FarEastLineBreakControl = sourceStyle.ParagraphFormat.FarEastLineBreakControl;
                targetStyle.ParagraphFormat.FirstLineIndent = sourceStyle.ParagraphFormat.FirstLineIndent;
                targetStyle.ParagraphFormat.HalfWidthPunctuationOnTopOfLine = sourceStyle.ParagraphFormat.HalfWidthPunctuationOnTopOfLine;
                targetStyle.ParagraphFormat.HangingPunctuation = sourceStyle.ParagraphFormat.HangingPunctuation;
                targetStyle.ParagraphFormat.Hyphenation = sourceStyle.ParagraphFormat.Hyphenation;
                targetStyle.ParagraphFormat.KeepTogether = sourceStyle.ParagraphFormat.KeepTogether;
                targetStyle.ParagraphFormat.KeepWithNext = sourceStyle.ParagraphFormat.KeepWithNext;
                targetStyle.ParagraphFormat.LeftIndent = sourceStyle.ParagraphFormat.LeftIndent;
                targetStyle.ParagraphFormat.LineSpacing = sourceStyle.ParagraphFormat.LineSpacing;
                targetStyle.ParagraphFormat.LineSpacingRule = sourceStyle.ParagraphFormat.LineSpacingRule;
                targetStyle.ParagraphFormat.LineUnitAfter = sourceStyle.ParagraphFormat.LineUnitAfter;
                targetStyle.ParagraphFormat.LineUnitBefore = sourceStyle.ParagraphFormat.LineUnitBefore;
                targetStyle.ParagraphFormat.MirrorIndents = sourceStyle.ParagraphFormat.MirrorIndents;
                targetStyle.ParagraphFormat.NoLineNumber = sourceStyle.ParagraphFormat.NoLineNumber;
                targetStyle.ParagraphFormat.OutlineLevel = sourceStyle.ParagraphFormat.OutlineLevel;
                targetStyle.ParagraphFormat.PageBreakBefore = sourceStyle.ParagraphFormat.PageBreakBefore;
                targetStyle.ParagraphFormat.ReadingOrder = sourceStyle.ParagraphFormat.ReadingOrder;
                targetStyle.ParagraphFormat.RightIndent = sourceStyle.ParagraphFormat.RightIndent;
                targetStyle.ParagraphFormat.SpaceAfter = sourceStyle.ParagraphFormat.SpaceAfter;
                targetStyle.ParagraphFormat.SpaceAfterAuto = sourceStyle.ParagraphFormat.SpaceAfterAuto;
                targetStyle.ParagraphFormat.SpaceBefore = sourceStyle.ParagraphFormat.SpaceBefore;
                targetStyle.ParagraphFormat.SpaceBeforeAuto = sourceStyle.ParagraphFormat.SpaceBeforeAuto;
                targetStyle.ParagraphFormat.TextboxTightWrap = sourceStyle.ParagraphFormat.TextboxTightWrap;
                targetStyle.ParagraphFormat.WordWrap = sourceStyle.ParagraphFormat.WordWrap;
                */
            }
        }

        private static void CopyStyleFont(Style sourceStyle, Style targetStyle)
        {

            targetStyle.Font = sourceStyle.Font;
            return;

            /*
            targetStyle.Font.AllCaps = sourceStyle.Font.AllCaps;
            targetStyle.Font.Animation = sourceStyle.Font.Animation;
            targetStyle.Font.Bold = sourceStyle.Font.Bold;
            targetStyle.Font.Color = sourceStyle.Font.Color;
            targetStyle.Font.ColorIndex = sourceStyle.Font.ColorIndex;
            targetStyle.Font.DiacriticColor = sourceStyle.Font.DiacriticColor;
            targetStyle.Font.DisableCharacterSpaceGrid = sourceStyle.Font.DisableCharacterSpaceGrid;
            targetStyle.Font.DoubleStrikeThrough = sourceStyle.Font.DoubleStrikeThrough;
            targetStyle.Font.Emboss = sourceStyle.Font.Emboss;
            targetStyle.Font.EmphasisMark = sourceStyle.Font.EmphasisMark;
            targetStyle.Font.Engrave = sourceStyle.Font.Engrave;
            targetStyle.Font.Hidden = sourceStyle.Font.Hidden;
            targetStyle.Font.Italic = sourceStyle.Font.Italic;
            targetStyle.Font.Kerning = sourceStyle.Font.Kerning;
            targetStyle.Font.Name = sourceStyle.Font.Name;
            targetStyle.Font.Outline = sourceStyle.Font.Outline;
            targetStyle.Font.Position = sourceStyle.Font.Position;
            targetStyle.Font.Scaling = sourceStyle.Font.Scaling;
            targetStyle.Font.Shadow = sourceStyle.Font.Shadow;
            targetStyle.Font.Size = sourceStyle.Font.Size;
            targetStyle.Font.SmallCaps = sourceStyle.Font.SmallCaps;
            targetStyle.Font.Spacing = sourceStyle.Font.Spacing;
            targetStyle.Font.StrikeThrough = sourceStyle.Font.StrikeThrough;
            targetStyle.Font.Subscript = sourceStyle.Font.Subscript;
            targetStyle.Font.Superscript = sourceStyle.Font.Superscript;
            targetStyle.Font.Underline = sourceStyle.Font.Underline;
            targetStyle.Font.UnderlineColor = sourceStyle.Font.UnderlineColor;
            */


        }

        #endregion

        #region Page Setup

        public static void CopyPageSetup(Document sourceDocument, Document targetDocument)
        {
            //targetDocument.PageSetup = sourceDocument.PageSetup;
            //return;

            CopyPageSetupMargins(sourceDocument, targetDocument);
            CopyPageSetupTextColumns(sourceDocument, targetDocument);

            return;

            CopyPageSetupBookFoldPrinting(sourceDocument, targetDocument);

            //targetDocument.PageSetup.CharsLine = sourceDocument.PageSetup.CharsLine;
            targetDocument.PageSetup.DifferentFirstPageHeaderFooter = sourceDocument.PageSetup.DifferentFirstPageHeaderFooter;
            targetDocument.PageSetup.FirstPageTray = sourceDocument.PageSetup.FirstPageTray;

            CopyPageSetupHeaderFooterDistance(sourceDocument, targetDocument);
            
            CopyPageSetupGutter(sourceDocument, targetDocument);

            
            targetDocument.PageSetup.LayoutMode = sourceDocument.PageSetup.LayoutMode;
            targetDocument.PageSetup.LineNumbering = sourceDocument.PageSetup.LineNumbering;
            targetDocument.PageSetup.LinesPage = sourceDocument.PageSetup.LinesPage;
            targetDocument.PageSetup.MirrorMargins = sourceDocument.PageSetup.MirrorMargins;
            targetDocument.PageSetup.OddAndEvenPagesHeaderFooter = sourceDocument.PageSetup.OddAndEvenPagesHeaderFooter;
            targetDocument.PageSetup.Orientation = sourceDocument.PageSetup.Orientation;
            targetDocument.PageSetup.OtherPagesTray = sourceDocument.PageSetup.OtherPagesTray;

            CopyPageSetupPageSize(sourceDocument, targetDocument);

            targetDocument.PageSetup.PaperSize = sourceDocument.PageSetup.PaperSize;
            
            targetDocument.PageSetup.SectionDirection = sourceDocument.PageSetup.SectionDirection;
            targetDocument.PageSetup.SectionStart = sourceDocument.PageSetup.SectionStart;
            //targetDocument.PageSetup.ShowGrid = sourceDocument.PageSetup.ShowGrid;
            targetDocument.PageSetup.SuppressEndnotes = sourceDocument.PageSetup.SuppressEndnotes;

            

            targetDocument.PageSetup.TwoPagesOnOne = sourceDocument.PageSetup.TwoPagesOnOne;
            targetDocument.PageSetup.VerticalAlignment = sourceDocument.PageSetup.VerticalAlignment;
            


        }

        private static void CopyPageSetupPageSize(Document sourceDocument, Document targetDocument)
        {
            targetDocument.PageSetup.PageHeight = sourceDocument.PageSetup.PageHeight;
            targetDocument.PageSetup.PageWidth = sourceDocument.PageSetup.PageWidth;
        }

        private static void CopyPageSetupHeaderFooterDistance(Document sourceDocument, Document targetDocument)
        {
            targetDocument.PageSetup.HeaderDistance = sourceDocument.PageSetup.HeaderDistance;
            targetDocument.PageSetup.FooterDistance = sourceDocument.PageSetup.FooterDistance;
        }

        private static void CopyPageSetupGutter(Document sourceDocument, Document targetDocument)
        {
            targetDocument.PageSetup.Gutter = sourceDocument.PageSetup.Gutter;
            targetDocument.PageSetup.GutterOnTop = sourceDocument.PageSetup.GutterOnTop;
            targetDocument.PageSetup.GutterPos = sourceDocument.PageSetup.GutterPos;
            targetDocument.PageSetup.GutterStyle = sourceDocument.PageSetup.GutterStyle;
        }

        private static void CopyPageSetupTextColumns(Document sourceDocument, Document targetDocument)
        {
            // TODO : Make this work
            return;
            for (int i = 1; i <= sourceDocument.PageSetup.TextColumns.Count; i++)
            {
                TextColumn textColumn = targetDocument.PageSetup.TextColumns[i];
                object width = textColumn.Width;
                object spacing = sourceDocument.PageSetup.TextColumns.Spacing;
                object evenlySpaced = System.Reflection.Missing.Value; // sourceDocument.PageSetup.TextColumns.EvenlySpaced;
                targetDocument.PageSetup.TextColumns.Add(ref width, ref spacing, ref evenlySpaced);
            }

            //targetDocument.PageSetup.TextColumns
            targetDocument.PageSetup.TextColumns.EvenlySpaced = sourceDocument.PageSetup.TextColumns.EvenlySpaced;
            targetDocument.PageSetup.TextColumns.FlowDirection = sourceDocument.PageSetup.TextColumns.FlowDirection;
            targetDocument.PageSetup.TextColumns.LineBetween = sourceDocument.PageSetup.TextColumns.LineBetween;
            targetDocument.PageSetup.TextColumns.Spacing = sourceDocument.PageSetup.TextColumns.Spacing;
            targetDocument.PageSetup.TextColumns.Width = sourceDocument.PageSetup.TextColumns.Width;

        }

        private static void CopyPageSetupBookFoldPrinting(Document sourceDocument, Document targetDocument)
        {
            targetDocument.PageSetup.BookFoldPrinting = sourceDocument.PageSetup.BookFoldPrinting;
            targetDocument.PageSetup.BookFoldPrintingSheets = sourceDocument.PageSetup.BookFoldPrintingSheets;
            targetDocument.PageSetup.BookFoldRevPrinting = sourceDocument.PageSetup.BookFoldRevPrinting;
        }

        private static void CopyPageSetupMargins(Document sourceDocument, Document targetDocument)
        {
            targetDocument.PageSetup.TopMargin = sourceDocument.PageSetup.TopMargin;
            targetDocument.PageSetup.BottomMargin = sourceDocument.PageSetup.BottomMargin;
            targetDocument.PageSetup.LeftMargin = sourceDocument.PageSetup.LeftMargin;
            targetDocument.PageSetup.RightMargin = sourceDocument.PageSetup.RightMargin;
        }

        #endregion

    }
}
